package com.im.partchat.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;

import com.im.partchat.R;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.permission.PermissionListener;
import com.im.partchat.utils.Navigation;
import com.im.partchat.utils.rxlifecycle.compact.RxLifecycleCompact;
import com.im.partchat.widget.dialog.CommomDialog;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {


    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }



    @Override
    protected void initData() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("");
                e.onComplete();
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .delay(2, TimeUnit.SECONDS)
                .compose(RxLifecycleCompact.bind(SplashActivity.this).<String>withObservable())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        if (Build.VERSION.SDK_INT >= 23) {
                            requestPermissions(new String[]{
                                    Manifest.permission.CAMERA,
                                    Manifest.permission.RECORD_AUDIO,
                                    Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.READ_PHONE_STATE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.CALL_PHONE

                            }, new PermissionListener() {
                                @Override
                                public void onGranted() {
                                    Navigation.navigateLogin();
                                    finish();
                                }

                                @Override
                                public void onDenied(List<String> deniedPermissions) {
                                    for (String deniedPermission : deniedPermissions) {
                                        Log.d(TAG, "onDenied: "+deniedPermission);
                                    }
                                    CommomDialog commomDialog=new CommomDialog(SplashActivity.this);
                                    commomDialog
                                            .setTitle("请在设置界面开启权限后运行app")
                                            .setPositiveOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    Intent localIntent = new Intent();
                                                    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                    if (Build.VERSION.SDK_INT >= 9) {
                                                        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                                        localIntent.setData(Uri.fromParts("package", getApplicationContext().getPackageName(), null));
                                                    } else if (Build.VERSION.SDK_INT <= 8) {
                                                        localIntent.setAction(Intent.ACTION_VIEW);
                                                        localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                                                        localIntent.putExtra("com.android.settings.ApplicationPkgName", getApplicationContext().getPackageName());
                                                    }
                                                    startActivity(localIntent);
                                                    finish();
                                                }
                                            })
                                            .setNegativeOnClickListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View v) {
                                                    finish();
                                                }
                                            })
                                            .show();
                                }
                            });
                        }
                        else {
                            Navigation.navigateLogin();
                            finish();
                        }
                    }
                });

    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

    }

    @Override
    protected boolean hasToolBar() {
        return false;
    }

}
